#include <iostream>
#include <string>

using namespace std;

void PrintBoard();                                      // This function prints the Tic-Tac-Toe board, in original and updated versions
bool IsMoveLegal(char Move);                    // Checks to see if the move is legal
int TurnDecider(char PlayerChar);

char Tic1 = '1';                                // These global character variables represent each place on the TTT board
char Tic2 = '2';
char Tic3 = '3';
char Tic4 = '4';
char Tic5 = '5';
char Tic6 = '6';
char Tic7 = '7';
char Tic8 = '8';
char Tic9 = '9';

int main()
{
        int isGameOver = 1;                        // int variable changed from boolean for a move count to stop the game
        string Player1Name, Player2Name;        // These string variables grab the player's names and display them accordingly

        char PlayerMove;                                // This is the player move that will be translated into the various places on the board once entered
        char PlayerMark;                                // This character variable tells the compiler if it is P1 entering, == 'X' or P2 == 'O'

        cout << "\t\t\tWelcome to Tic-Tac-Toe!" << endl;
        cout << "\tIn this game, each of the two players will enter their name, then" << endl;
        cout << "\t\tPlayer 1 will be asked to enter his/her first guess." << endl;
        cout << "\tThen, the board will show up with each player's guess until the" << endl;
        cout << "\t\three-in-a-row is made out of the numbers!" << endl << endl;
        cout << "\t   Best of luck and may the best Tic-Tac-Toe'r win!" << endl << endl;

        cout << "Player 1, please enter your name: ";                   // Gathers Player 1's name
        getline(cin, Player1Name);
        cout << endl << "Thank you, " << Player1Name << endl << endl;

        cout << "Player 2, please enter your name: ";                   // Gathers Player 2's name
        getline(cin, Player2Name);
        cout << endl << "Thank you, " << Player2Name << endl << endl;

        PrintBoard();

        cout << Player1Name << ", please enter your move: ";    // Engage main game loop(?)
        cin >> PlayerMove;
		PlayerMark = 'X'; // moved up for initial player mark it will actually start as O.
        while (isGameOver < 10)
                PlayerMark = TurnDecider(char PlayerChar); // call to function TurnDecider that 
                if (IsMoveLegal(PlayerMove) == true)	   // determines the  mark each turn
                {
                        if (PlayerMove == '1')
                        {
                                Tic1 = PlayerMark;
								isGameOver = isGameOver + 1;
                        }
                        else if (PlayerMove == '2')
                        {
                                Tic2 = PlayerMark;
								isGameOver = isGameOver + 1;
                        }
                        else if (PlayerMove == '3')
                        {
                                Tic3 = PlayerMark;
								isGameOver = isGameOver + 1;
                        }
                        else if (PlayerMove == '4')
                        {
                                Tic4 = PlayerMark;
								isGameOver = isGameOver + 1;
                        }
                        else if (PlayerMove == '5')
                        {
                                Tic5 = PlayerMark;
								isGameOver = isGameOver + 1;
                        }
                        else if (PlayerMove == '6')
                        {
                                Tic6 = PlayerMark;
								isGameOver = isGameOver + 1;
                        }
                        else if (PlayerMove == '7')
                        {
                                Tic7 = PlayerMark;
								isGameOver = isGameOver + 1;
                        }
                        else if (PlayerMove == '8')
                        {
                                Tic8 = PlayerMark;
								isGameOver = isGameOver + 1;
                        }
                        else if (PlayerMove == '9')
                        {
                                Tic9 = PlayerMark;
								isGameOver = isGameOver + 1;
                        }
                        else 
						{
						cout << "invalid move try again" << endl;
						}
						
						PrintBoard(); 
					else
					{
					cout << "Move has already been taken, try again." << endl;  // added else to explain duplicate moves
					}
                }
        }

        system("PAUSE");
        return 0;
}

void PrintBoard()
{
        cout << "   " << Tic1 << "|" << Tic2 << "|" << Tic3 << endl;
        cout << "   - - -" << endl;
        cout << "   " << Tic4 << "|" << Tic5 << "|" << Tic6 << endl;
        cout << "   - - -" << endl;
        cout << "   " << Tic7 << "|" << Tic8 << "|" << Tic9 << endl << endl;
}

bool IsMoveLegal(char Move)
{
        char PlayerMove;
        PlayerMove = Move;

        if (PlayerMove == '1' || PlayerMove == '2' || PlayerMove == '3' ||
                PlayerMove == '4' || PlayerMove == '5' || PlayerMove == '6' ||
                PlayerMove == '7' || PlayerMove == '8' || PlayerMove == '9')
        {
                if (PlayerMove == '1' && Tic1 == '1')
                {
                        return true;
                }
                else if (PlayerMove == '2' && Tic2 == '2')
                {
                        return true;
                }
                else if (PlayerMove == '3' && Tic3 == '3')
                {
                        return true;
                }
                else if (PlayerMove == '4' && Tic4 == '4')
                {
                        return true;
                }
                else if (PlayerMove == '5' && Tic5 == '1')
                {
                        return true;
                }
                else if (PlayerMove == '6' && Tic6 == '2')
                {
                        return true;
                }
                else if (PlayerMove == '7' && Tic7 == '1')
                {
                        return true;
                }
                else if (PlayerMove == '8' && Tic8 == '2')
                {
                        return true;
                }
                else if (PlayerMove == '9' && Tic9 == '1')
                {
                        return true;
                }
                else
                {
                        return false;
                }
        }
        else
        {
                return false;
        }
}

int TurnDecider(char PlayerChar)
{
      if ( PlayerMark == "X")
	  PlayerChar = "O";
	  else
	  PlayerChar = "X";
	  
	  return PlayerChar;
	  
}

